package welsch.androidwerwolf.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import welsch.androidwerwolf.Player;
import welsch.androidwerwolf.R;

/**
 * Created by loki on 10.01.2015.
 */
public class PlayerListAdapter  extends ArrayAdapter<Player>{

    private List<Player> cPlayerList = new ArrayList<>();
    private int layoutResourceId;
    private Context context;

    public PlayerListAdapter(Context context, int layoutResourceId, List<Player> items) {
        super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.cPlayerList = items;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final PlayerHolder holder = new PlayerHolder();;

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder.player = cPlayerList.get(position);
        holder.removePaymentButton = (ImageButton)row.findViewById(R.id.atomPay_removePay);
        holder.removePaymentButton.setTag(holder.player);
        holder.name = (TextView)row.findViewById(R.id.atomPay_name);
        holder.name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                holder.player.setName(s.toString());

            }
        });
        row.setTag(holder);

        setupItem(holder);
        return row;
    }

    private void setupItem(PlayerHolder holder) {
        holder.name.setText(holder.player.getName());
    }

    public static class PlayerHolder {
        Player player;
        TextView name;
        ImageButton removePaymentButton;
    }
}

